import request from '@/utils/request'

import type {
  SpuResponseData,
  SpuImageResponseData,
  SpuSaleAttrResponseData,
  BaseSaleAttrData,
  SpuRecord,
} from './type'
enum API {
  //获取Spu分页数据地址
  GetSpuPageList_URL = '/admin/product/',
  //获取指定分类下的SPU图像列表地址
  GetSpuImageList_URL = '/admin/product/spuImageList/',
  //获取指定spu下的销售属性地址
  GetSpuSaleAttr_URL = '/admin/product/spuSaleAttrList/',
  //获取所有的销售属性
  GetBaseSaleAttr_URL = '/admin/product/baseSaleAttrList/',
  //spu保存地址
  SaveSpu_URL = '/admin/product/saveSpuInfo',
  //spu更新地址
  UpdateSpu_URL = '/admin/product/updateSpuInfo',
  //spu删除
  DeleteSpu_URL = '/admin/product/deleteSpu/',
}

//获取Spu分页数据
export const GetSpuPageList = (
  currentPage: number,
  pageSize: number,
  category3Id: string | number,
) =>
  request.get<any, SpuResponseData>(
    API.GetSpuPageList_URL +
      `${currentPage}/${pageSize}?category3Id=${category3Id}`,
  )

//获取指定SPU图像列表
export const GetSpuImageList = (spuId: number) =>
  request.get<any, SpuImageResponseData>(API.GetSpuImageList_URL + spuId)
//获取指定SPU下的销售属性
export const GetSpuSaleAttrList = (spuId: number) =>
  request.get<any, SpuSaleAttrResponseData>(API.GetSpuSaleAttr_URL + spuId)
//获取全部销售属性
export const GetBaseSaleAttrList = () =>
  request.get<any, BaseSaleAttrData>(API.GetBaseSaleAttr_URL)
//新增|修改SPU
export const SaveOrUpdateSpu = (data: SpuRecord) => {
  if (data.id) {
    return request.post<any, any>(API.UpdateSpu_URL, data)
  } else {
    return request.post<any, any>(API.SaveSpu_URL, data)
  }
}

//删除指定SPU
export const DeleteSpu = (spuId: number) =>
  request.delete<any, any>(API.DeleteSpu_URL + spuId)
